<template>
	<picker mode="multiSelector" @change="MultiChange" @columnchange="MultiColumnChange" :value="multiIndex" :range="academies" range-key="name">
		<view class="picker">
			{{academies[0][multiIndex[0]].name}},{{academies[1][multiIndex[1]].name}},{{academies[2][multiIndex[2]].name}},{{academies[3][multiIndex[3]].name}}
		</view>
	</picker>
</template>

<script>
	export default {
		name:"school-picker",
		data() {
			return {
				multiIndex:[0,0,0,0],
				academies:[]
			};
		},
		props: {
			academy: {
				type: Array,
				default: []
			},
			schoolId:{
				type:Number,
				default:1
			},
			oldClassesIndex:{
				type:Number,
				default:0
			}
		},
		created() {
			this.academies=this.academy
			if(this.oldClassesIndex>0){
				this.multiIndex[3]=this.oldClassesIndex
			}
		},
		methods:{
			//确认选择项
			MultiChange(e) {
				this.multiIndex = e.detail.value
				
				let data={}
				
				data.academy=this.academies[0][this.multiIndex[0]]
				data.department=this.academies[1][this.multiIndex[1]]
				data.major=this.academies[2][this.multiIndex[2]]
				data.classes=this.academies[3][this.multiIndex[3]]
				
				this.$emit("change",data)
			},
			//选择列
			MultiColumnChange(e) {
				let data = {
					multiIndex: this.multiIndex
				};
				data.multiIndex[e.detail.column] = e.detail.value;
				this.multiIndex = data.multiIndex;
				
				let column=e.detail.column
				
				if(column!=3){
					this.SearchSchoolData(column+1);
				}
			},
			SearchSchoolData(type){
				let academyId=this.academies[0][this.multiIndex[0]].id
				let departmentId=this.academies[1][this.multiIndex[1]].id
				let majorId=this.academies[2][this.multiIndex[2]].id
				
				let data={
					searchType:type,
					schoolId:this.schoolId,
					academyId:academyId,
					departmentId:departmentId,
					majorId:majorId
				}
				
				let _this=this
				
				this.$api.post('/Schoolacademy/search_list',data).then(res => {
						
					if(type==1){
						this.multiIndex=[this.multiIndex[0],0,0,0]
					}else if(type==2){
						this.multiIndex=[this.multiIndex[0],this.multiIndex[1],0,0]
					}else if(type==3){
						this.multiIndex=[this.multiIndex[0],this.multiIndex[1],this.multiIndex[2],0]
					}
					
					let data=res.data
					
					if(data.departments){
						_this.$set(_this.academies,1,data.departments)
						_this.department=data.departments[0]
					}
					if(data.majors){
						_this.$set(_this.academies,2,data.majors)
						_this.major=data.majors[0]
					}						
					
					_this.$set(_this.academies,3,data.classes)						
					_this.classes=data.classes[0]
						
				}).catch(()=>{
					confirm.error("获取数据错误!!")
				})
			},
		}
	}
</script>

<style>

</style>